#include <cmath>
#include "nr_c/nr.h"
#include "function.h"
#include <string>

static Function *CGMfunction;

// used only in this file to do CGM
static double zzfunc(double *x)  
{
  double val;
  val = CGMfunction->Value(x);

  return (val);
  //return (CGMfunction->NormValue(x));
}

static void zzofunc(double *x, string& directory, int i)
{
  int runNum = i;
  double value;
  value = CGMfunction->Value(x);

  char name[80];

  sprintf_s(name, 80, string(directory + "CGM_result_struc%d.txt").c_str(), runNum);
  CGMfunction->print_sol(x, value, name, 0, 0);
}


// used only in this file to do CGM
static void zzdfunc(double *x, double *y)
{ 
  CGMfunction->BuildDerivative(x,y);
}


/*void wrapperCGM(double *solLocalMinimize, int dim, double ftol, double *funcVal, Function* func, string& directory, double *pstruc, double *pstrucval)
{
  CGMfunction = func;

  int iter;

  frprmn(solLocalMinimize, dim, ftol, &iter, funcVal,zzfunc,zzofunc,zzdfunc, directory, pstruc, pstrucval);
}*/

void wrapperCGM(double *solLocalMinimize, int dim, double ftol, double *funcVal, Function* func, string& directory)
{
  CGMfunction = func;

  int iter;

  frprmn(solLocalMinimize, dim, ftol, &iter, funcVal,zzfunc,zzdfunc, directory);
}